/**
 * 
 */
package CCRMClient.GUI;

import javax.swing.JPanel;
import javax.swing.JLabel;

import java.awt.Font;
import java.awt.Color;

import javax.swing.JTextArea;
import javax.swing.JButton;

import CCRMClient.ClientStart;
import Common.SERVER_COMMANDS;
import Common.Entities.CustomerResponse;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.io.IOException;
import java.awt.SystemColor;

/**
 * This screen is for customer refusal when pressed "Submit" 
 * @author Oz Mouchtar
 *
 */
public class CustomerRefusalScreen extends JPanel {
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private JLabel lblRefusal;
	private JLabel lblPleaseFillThe;
	private JTextArea txtrReason;
	private JButton btnSubmit;
	private JButton btnGoBack;
	private CustomerResponse response;
	public CustomerRefusalScreen(CustomerResponse response) {
		this.response = response; 
		initComponents();
	}

	private void initComponents() {
		setBackground(Color.WHITE);
		setLayout(null);
		
		lblRefusal = new JLabel("Refusal");
		lblRefusal.setForeground(Color.RED);
		lblRefusal.setFont(new Font("David", Font.BOLD, 26));
		lblRefusal.setBounds(185, 23, 114, 27);
		add(lblRefusal);
		
		lblPleaseFillThe = new JLabel("Please fill the reason for the refusal:");
		lblPleaseFillThe.setFont(new Font("David", Font.BOLD, 16));
		lblPleaseFillThe.setBounds(10, 73, 255, 14);
		add(lblPleaseFillThe);
		
		txtrReason = new JTextArea();
		txtrReason.setBackground(SystemColor.control);
		txtrReason.setLineWrap(true);
		txtrReason.setBounds(10, 98, 430, 212);
		add(txtrReason);
		
		btnSubmit = new JButton("Submit");
		btnSubmit.addActionListener(new BtnSubmitActionListener());
		btnSubmit.setFont(new Font("Tahoma", Font.BOLD, 11));
		btnSubmit.setBounds(351, 321, 89, 23);
		add(btnSubmit);
		
		btnGoBack = new JButton("Go back");
		btnGoBack.addActionListener(new BtnGoBackActionListener());
		btnGoBack.setFont(new Font("Tahoma", Font.BOLD, 11));
		btnGoBack.setBounds(10, 321, 89, 23);
		add(btnGoBack);
	}
	private class BtnGoBackActionListener implements ActionListener {
		public void actionPerformed(ActionEvent arg0) {
			try {
				ClientStart.client.handleMessageFromGUI(SERVER_COMMANDS.ADDRESS_CUSTOMER, response.getCustomerID() + " " + response.getSaleID());
			} catch (IOException e) {
				// TODO Auto-generated catch block
				ClientStart.appWindow.notifyUser(e.getMessage(), "Refusal", "ERROR_MESSAGE");
			}
		}
	}
	private class BtnSubmitActionListener implements ActionListener {
		public void actionPerformed(ActionEvent e) {
			try {
				if(txtrReason.getText().isEmpty())
					ClientStart.appWindow.notifyUser("Please write the reason for the refusal", "Refusal", "ERROR_MESSAGE");
				response.setResponseRefusalDescription(txtrReason.getText());
				ClientStart.client.handleMessageFromGUI(SERVER_COMMANDS.ADD_RESPONSE, response);
			} catch (IOException e1) {
				// TODO Auto-generated catch block
				ClientStart.appWindow.notifyUser(e1.getMessage(), "Refusal", "ERROR_MESSAGE");
			}
		}
	}
}
